// components/wu-pay/wu-pay.js

var _this = {
    self: {
        show() {

        }
    }
};

wx.wuRequestPayment = function () {
    //arguments实参构成的伪数组
    var _ref = arguments.length > 0 && arguments[0] != 'undefined' ? arguments[0] : {},
        timeStamp = _ref.timeStamp,
        nonceStr = _ref.nonceStr,
        _package = _ref._package,
        signType = _ref.signType,
        paySign = _ref.paySign,
        success = _ref.success,
        fail = _ref.fail

    _this.timeStamp = timeStamp;
    _this.nonceStr = nonceStr;
    // prepay_id = wxsdkflkadlkf
    _this._package = _package;
    _this.signType = signType;
    _this.paySign = paySign;
    _this.success = success;
    _this.fail = fail;

    if (_package.split("=").length != 2 || paySign.split("##").length != 6) {
        _this.fail({
            errMsg: "参数错误"
        })
        return
    }
    _this.self.show()
}

Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        title: "",//商品名称
        total: "0.00",//商品总价
        show: false,//决定组件显示隐藏
        open: false,//决定数字键盘显示隐藏
        success: false,//决定成功页面展示的
        pw: [],//临时存放密码
        tempRes: null
    },

    lifetimes: {
        created() {
            //组件编译完成之后
            _this.self = this
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        show() {
            // 获取title和total
            let arr = _this.paySign.split("##");
            let title = arr[0]
            let total = arr[1]

            this.setData({
                show: true,
                title,
                total,
            })
            setTimeout(() => {
                this.setData({
                    open: true
                })
            }, 300)
        },
        keyboard(_ref2) {
            //点击下面的数字键盘触发的
            var num = _ref2.target.dataset.num;

            if (num === undefined) return;
            if (num === '-1' && this.data.pw.length) {
                this.data.pw.pop();
            }
            if (num !== undefined && num !== '-1' && this.data.pw.length < 6) {
                this.data.pw.push(num);
            }
            this.setData({
                pw: this.data.pw
            });
            this.check();
        },
        check() {
            // 比对密码的
            var that = this;
            if (this.data.pw.length === 6) {
                wx.showLoading({
                    title: '正在支付',
                    mask: true
                });
                wx.request({
                    url: "http://127.0.0.1:2000/pay",
                    method: "POST",
                    header: {
                        toke: "token110"
                    },
                    data: {
                        order_num: "wx110"
                    },
                    success(res) {
                        setTimeout(() => {
                            wx.hideLoading();

                            that.setData({
                                success: true
                            })
                        }, 1500)
                    }
                })
            }
        },
        // 点x关闭支付
        close() {
            this.setData({
                show: !this.data.show
            })
        },
        success() {
            wx.switchTab({
                url: '/pages/index/index'
            })
        }
    }
})
